package com.mingtu.guards.activity;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.bean.EcDequiBean;
import com.mingtu.guards.fragment.EcHydroModuleFragment;
import com.mingtu.guards.fragment.EcWeatherModuleFragment;
import com.mingtu.guards.utils.MyConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

public class EcRealtimeDataActivity extends MyBaseActivity {


    @BindView(R.id.slide_tab)
    CommonTabLayout commonTab;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    //    @BindView(R.id.srl_up)
//    SmartRefreshLayout srlUp;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"气象", "水文水质"};
    private int navPosition = 0;
    private EcWeatherModuleFragment ecWeatherModuleFragment;
    private EcHydroModuleFragment ecHydroModuleFragment;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private int position;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ec_realtime_data;
    }

    @Override
    protected void initView() {
        setModuleTitle("实时监测");
        super.initView();
    }

    @Override
    protected void initData() {
        position = getIntent().getIntExtra("type", 0);

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }


//        initListener();

    }

    @Override
    protected void getData() {
        getDequiList();
    }


//    private void initListener() {
//        initPullRefresh();//刷新
//    }
//
//    private void initPullRefresh() {
//        srlUp.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
//        srlUp.setEnableLoadMore(false);
//        srlUp.setEnableOverScrollDrag(true);
//        srlUp.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                srlUp.finishRefresh(Constant.REFRESH_TIME);
//                srlUp.setNoMoreData(false);
////                getDequiList();
//            }
//        });
//    }

    private class MyPagerAdapter extends LazyFragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }


        @Override
        protected Fragment getItem(ViewGroup viewGroup, int position) {
            return mFragments.get(position);
        }
    }


    //点击tab 监听
    private class MyOnTabSelectListener implements OnTabSelectListener {

        @Override
        public void onTabSelect(int position) {
            viewPager.setCurrentItem(position);
            navPosition = position;
        }

        @Override
        public void onTabReselect(int position) {

        }
    }


    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            commonTab.setCurrentTab(position);
            navPosition = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };


    /**
     * 设备列表
     */
    private void getDequiList() {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deviceType", null);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_EC_DEVICE)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            EcDequiBean ecDequiBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                ecDequiBean = new EcDequiBean();
                                ecDequiBean = gson.fromJson(result, EcDequiBean.class);
                                List<EcDequiBean.DataBean> dataBeans = ecDequiBean.getData();
                                ArrayList<EcDequiBean.DataBean> weatherDequiList = new ArrayList<>();
                                ArrayList<EcDequiBean.DataBean> hydroDequiList = new ArrayList<>();
                                if (dataBeans != null) {
                                    int size = dataBeans.size();
                                    if (size > 0) {
                                        for (int i = 0; i < size; i++) {
                                            EcDequiBean.DataBean dataBean = dataBeans.get(i);
                                            String name = dataBean.getName();
                                            if (!StringUtils.isEmpty(name) && name.indexOf("气象") != -1) {
                                                weatherDequiList.add(dataBean);
                                            }
                                            if (!StringUtils.isEmpty(name) && name.indexOf("水文") != -1) {
                                                hydroDequiList.add(dataBean);
                                            }
                                        }
                                        ecWeatherModuleFragment = EcWeatherModuleFragment.getInstance(weatherDequiList);
                                        ecHydroModuleFragment = EcHydroModuleFragment.getInstance(hydroDequiList);

                                        mFragments.add(ecWeatherModuleFragment);
                                        mFragments.add(ecHydroModuleFragment);

                                        viewPager.setOffscreenPageLimit(mFragments.size());//需要设置此值
                                        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
                                        viewPager.setCurrentItem(position);
                                        viewPager.addOnPageChangeListener(onPageChangeListener);

                                        commonTab.setTextBold(1);
                                        commonTab.setTabData(mTabEntities);
                                        commonTab.setCurrentTab(position);
                                        commonTab.setOnTabSelectListener(new MyOnTabSelectListener());

                                    }
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

}